﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using SalaryChannel.SCQueryService;
using System.Collections.ObjectModel;

namespace SalaryChannel
{
    public partial class UploadData : UserControl
    {
        static readonly Char SplitChar = (char)9;//tab

        public void SetVisibility(Visibility Visible)
        {
            this.Visibility = Visible;
            if (Visible == Visibility.Collapsed)
            {
                this.FileTB.Text = string.Empty;
               
            }
        }

   
        private int rows = 0;
        public UploadData()
        {
            InitializeComponent();

            TableCBox.Items.Add("酬金");
            TableCBox.Items.Add("酬金池");


            ServiceInstance.QueryClient.ImportDataCompleted += new EventHandler<ImportDataCompletedEventArgs>(dclient_ImportDataCompleted);
            
        }


       

        private FileInfo dataFI = null;
        private void m_ImportBtn_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Filter = "文本文件(*.txt)|*.txt";
            fDialog.Multiselect = false;
            if (fDialog.ShowDialog().Value)
            {
                try
                {
                    
                    this.FileTB.Text = fDialog.File.Name;
                    dataFI = fDialog.File;
                   
                }
                catch (Exception ex)
                {
                   App.MessageBox.ShowDialog("导入数据失败：" + ex.Message, Title.Error);
                   
                }
            }
        }

        private void m_CommitBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.TableCBox.SelectedItem == null
                    || this.FileTB.Text == null)
                {
                    App.MessageBox.ShowDialog("请先选择目标表！", Title.Warning);
                    return;
                }
                if (this.TableCBox.SelectedItem.ToString().Equals("酬金"))
                {
                    tableName = "Salary_Mon";
                }
                else if (this.TableCBox.SelectedItem.ToString().Equals("酬金池"))
                {
                    tableName = "Salary_Pool";
                }

                using (StreamReader reader =  dataFI.OpenText())
                {// 
                  
                    string line = string.Empty;
                    try
                    {
                        while (!reader.EndOfStream)
                        {
                            line = reader.ReadLine();
                            if (string.IsNullOrEmpty(line))
                            {
                                continue;
                            }
                            string[] colHeaders = line.Split(SplitChar);
                            if (tableName == "Salary_Mon")
                            {
                                //colHeaders[0] == "统计月份"
                                //&& colHeaders[1] == "渠道编号"
                                //&& colHeaders[2] == "本月任务"
                                //&& colHeaders[3] == "全球通新增_户"
                                //&& colHeaders[4] == "动感地带新增_户"
                                //&& colHeaders[5] == "神州行新增_户"
                                //&& colHeaders[6] == "随E行八十元套餐新增_户"
                                //&& colHeaders[7] == "商务新增_户"
                                //&& colHeaders[8] == "家园卡2010版新增_户"
                                //&& colHeaders[9] == "创业一百新增_户"
                                //&& colHeaders[10] == "其他创业卡新增_户"
                                //&& colHeaders[11] == "新增合计_户"
                                //&& colHeaders[12] == "新增酬金合计_元"
                                //&& colHeaders[13] == "净增数_户"
                                //&& colHeaders[14] == "净增酬金_元"
                                //&& colHeaders[15] == "代收话费酬金_元"
                                //&& colHeaders[16] == "售卡酬金_元"
                                //&& colHeaders[17] == "新赠机_元"
                                //&& colHeaders[18] == "老赠机_元"
                                //&& colHeaders[19] == "承建费_元"
                                //&& colHeaders[20] == "代收百分之二_元"
                                //&& colHeaders[21] == "岁末_元"
                                //&& colHeaders[22] == "酬金合计_元"
                                //&& colHeaders[23] == "离网率"
                                //&& colHeaders[24] == "考核得分"
                                //&& colHeaders[25] == "考核后酬金_元"
                                //&& colHeaders[26] == "自购终端加百分之一提成_元"
                                //&& colHeaders[27] == "房屋补贴_元"
                                //&& colHeaders[28] == "补发_元"
                                //&& colHeaders[29] == "店员积分奖励_元"
                                //&& colHeaders[30] == "本月应发酬金_元"
                                //&& colHeaders[31] == "扣税_元"
                                //&& colHeaders[32] == "扣除酬金池金额_元"
                                //&& colHeaders[33] == "本月实发_元"
                                if (!(                                    
                                     colHeaders[0] =="统计月份"
                                    && colHeaders[1] =="渠道编号"
                                    && colHeaders[2] =="任务"
                                    && colHeaders[3] =="新增"
                                    && colHeaders[4] =="得分"
                                    && colHeaders[5] =="一返"
                                    && colHeaders[6] =="一返酬金"
                                    && colHeaders[7] =="一返应发"
                                    && colHeaders[8] =="二返"
                                    && colHeaders[9] =="二返酬金"
                                    && colHeaders[10] =="代收酬金"
                                    && colHeaders[11] =="空充酬金"
                                    && colHeaders[12] =="有价卡"
                                    && colHeaders[13] =="店员积分"
                                    && colHeaders[14] =="基础酬金"
                                    && colHeaders[15] =="超额奖励"
                                    && colHeaders[16] =="服务扣罚"
                                    && colHeaders[17] =="自购终端提成"
                                    && colHeaders[18] =="房屋补贴"
                                    && colHeaders[19] =="数据业务体验店终端补助"
                                    && colHeaders[20] =="数据业务体验店无线网卡补助"
                                    && colHeaders[21] =="手机缴费通酬金"
                                    && colHeaders[22] =="补发"
                                    && colHeaders[23] =="奖励扣补合计"
                                    && colHeaders[24] =="考核后酬金"
                                    && colHeaders[25] =="上月扣补"
                                    && colHeaders[26] =="其他酬金1"
                                    && colHeaders[27] =="其他酬金2"
                                    && colHeaders[28] =="其他酬金3"
                                    && colHeaders[29] =="本月合计"
                                    && colHeaders[30] =="活动酬金"
                                    && colHeaders[31] =="总税金"
                                    && colHeaders[32] =="酬金池当月扣发"
                                    && colHeaders[33] =="本月实发"
                                    ))
                                {
                                    App.MessageBox.ShowDialog("导入数据不合法，请检查标题行的数据是否合法！", Title.Warning);
                                    return;
                                }
                                break;
                            }
                            else
                            {
                                if (!("统计月份" == colHeaders[0]
                                && "渠道编号" == colHeaders[1]
                                && "本月渠道扣除金额_元" == colHeaders[2]
                                && "本月公司补贴金额_元" == colHeaders[3]
                                && "本月酬金池合计_元" == colHeaders[4]))
                                {
                                    App.MessageBox.ShowDialog("导入数据不合法，请检查标题行的数据是否合法！", Title.Warning);
                                    return;
                                }
                                break;
                            }
                        }
                        while (!reader.EndOfStream)
                        {
                            line = reader.ReadLine();
                            if (string.IsNullOrEmpty(line))
                            {
                                continue;
                            }
                            //MessageBox.Show(line);
                           
                            dataRows.Add(line);
                        }
                    }
                    catch (Exception ex)
                    {
                        App.MessageBox.ShowDialog("读取文件失败:" + ex.Message, Title.Error);
                        dataRows = null;
                    }
                    finally
                    {
                        try
                        {
                            reader.Close();
                            reader.Dispose();
                        }
                        catch 
                        {
                            
                        }
                    }
                    
                }
                if (dataRows.Count == 0)
                {
                    App.MessageBox.ShowDialog("导入的文件无有效数据，请检查",Title.Warning);
                    return;
                }
              
                ServiceInstance.QueryClient.ImportDataAsync(dataRows.Take(max_upload_rows).ToList<string>(),tableName, Guid.NewGuid().ToString());
                rows = dataRows.Count;
                ImportProgerssBar.Show();
                this.Cursor = Cursors.Wait;
               // this.IsEnabled = false;
            }
            catch 
            {
                this.Cursor = Cursors.Arrow;
                App.MessageBox.ShowDialog(Properties.Resource.ConnectServerFailed, Title.Error);
               
            }
        }

        List<string> dataRows = new List<string>();
        private readonly int max_upload_rows = 300;

        string tableName = string.Empty;

        public void Clear()
        {
            dataRows.Clear();
            tableName = string.Empty;
        }

        void dclient_ImportDataCompleted(object sender, ImportDataCompletedEventArgs e)
        {
            try
            {
                if (e.Result=="true")
                {
                    if (dataRows.Count > max_upload_rows)
                    {
                        dataRows.RemoveRange(0, max_upload_rows);
                        ServiceInstance.QueryClient.ImportDataAsync(dataRows.Take(max_upload_rows).ToList<string>(), tableName, Guid.NewGuid().ToString());
                    }
                    else
                    {
                        string msg = string.Format("数据导入成功！共导入{0}行数据.", rows);
                        ImportProgerssBar.Stop();
                        this.Cursor = Cursors.Arrow;
                        Clear(); 
                        //this.IsEnabled = true;
                        App.MessageBox.ShowDialog(msg, Title.Information);
                    }
                }
                else
                {
                    ImportProgerssBar.Stop();
                    this.Cursor = Cursors.Arrow;
                    Clear();
                    //this.IsEnabled = true;
                    App.MessageBox.ShowDialog("导入数据失败,"+ e.Result, Title.Error);
                }
            }
            catch (Exception ex)
            {

                ImportProgerssBar.Stop();
                this.Cursor = Cursors.Arrow;
                Clear();
                //this.IsEnabled = true;
                App.MessageBox.ShowDialog( "服务器发生异常：" + ex.Message,Title.Error);

            }
           
        }
    }

 
}
